//
//  UIEcgBackgroundView.m
//  DoubleECG
//
//  Created by Hawk on 2019/6/25.
//  Copyright © 2019 Hanmj. All rights reserved.
//

#import "UIECGBackgroundView.h"

@implementation UIECGBackgroundView
@synthesize bg5mmColor;
@synthesize bg1mmColor;
@synthesize background_color;
@synthesize bg1mmWidth;
@synthesize bg5mmWidth;
@synthesize bg10mmWidth,bg10mmColor;
@synthesize pixPermm;


-(void)setReFresh{
#if TARGET_OS_IOS
    [self setNeedsDisplay];
#else
    [self setNeedsDisplay:YES];
#endif
}

#pragma mark - Value set
-(void)setBg5mmWidth:(CGFloat)_background_strong_line_width{
    bg5mmWidth = _background_strong_line_width;
    [self setReFresh];
}

#if TARGET_OS_IOS
-(void)setBg5mmColor:(UIColor *)_background_strong_line_color{
#else
-(void)setBackground_strong_line_color:(NSColor *)_background_strong_line_color{
#endif
    bg5mmColor = _background_strong_line_color;
    [self setReFresh];
    
}

-(void)setBg1mmWidth:(CGFloat)_background_weak_line_width{
    bg1mmWidth = _background_weak_line_width;
    [self setReFresh];
}
    
#if TARGET_OS_IOS
-(void)setBg1mmColor:(UIColor *)_background_weak_line_color{
#else
-(void)setBackground_weak_line_color:(NSColor *)_background_weak_line_color{
#endif
    bg1mmColor = _background_weak_line_color;
    [self setReFresh];
}

-(void)setBg10mmWidth:(CGFloat)_background_board_line_width{
    bg10mmWidth =  _background_board_line_width;
    [self setReFresh];
}
#if TARGET_OS_IOS
-(void)setBg10mmColor:(UIColor *)_background_board_line_color{
#else
-(void)setBackground_board_line_color:(NSColor *)_background_board_line_color{
#endif
    bg10mmColor = _background_board_line_color;
    [self setReFresh];
}

    
#if TARGET_OS_IOS
-(void)setBackground_color:(UIColor *)_background_Color{
    background_color = _background_Color;
    self.backgroundColor = background_color;
}
#else
-(void)setBackground_color:(NSColor *)_background_Color{
    background_color = _background_Color;
    self.layer.backgroundColor = background_color.CGColor;
}
#endif
    
    
-(void)setPixPermm:(float)_pixPermm{
    pixPermm = _pixPermm;
    [self setReFresh];
}

#pragma mark - System Call
-(void)setFrame:(CGRect)frame{
    [super setFrame:CGRectIntegral(frame)];
    [self setReFresh];
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    [self draw_small_line];
    [self draw_strong_line];
    [self draw_board_line];
}

-(id)init{
    self = [super init];
    if (self){
        [self initData];
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectIntegral(frame)];
    if (self){
        [self initData];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self){
        [self initData];
    }
    return self;
}

#pragma mark - My Function
-(void)draw_small_line{
#if TARGET_OS_IOS
    CGContextRef context = UIGraphicsGetCurrentContext();
#else
    CGContextRef context = [NSGraphicsContext currentContext].CGContext;
#endif
    CGContextSetStrokeColorWithColor(context, bg1mmColor.CGColor);
    CGContextSetLineWidth(context, bg1mmWidth);
    
    float x = 1.f;
    while (x < self.frame.size.width){
        CGContextMoveToPoint(context, x, 1.f);
        CGContextAddLineToPoint(context, x, self.frame.size.height);
        x += pixPermm;
    }
    
    float y = 1.f;
    while (y < self.frame.size.height){
        CGContextMoveToPoint(context, 1.f, y);
        CGContextAddLineToPoint(context,self.frame.size.width, y);
        y += pixPermm;
    }
    CGContextStrokePath(context);
}

-(void)draw_strong_line{
    #if TARGET_OS_IOS
        CGContextRef context = UIGraphicsGetCurrentContext();
    #else
        CGContextRef context = [NSGraphicsContext currentContext].CGContext;
    #endif
    CGContextSetStrokeColorWithColor(context, bg5mmColor.CGColor);
    CGContextSetLineWidth(context, bg5mmWidth);
    
    float x = 1.f;
    while (x < self.frame.size.width){
        CGContextMoveToPoint(context, x, 1.f);
        CGContextAddLineToPoint(context, x, self.frame.size.height);
        x += 5 * pixPermm;
    }
    
    float y = 1.f;
    while (y < self.frame.size.height){
        CGContextMoveToPoint(context, 1.f, y);
        CGContextAddLineToPoint(context,self.frame.size.width, y);
        y += 5 * pixPermm;
    }
    CGContextStrokePath(context);
}

-(void)draw_board_line{
    #if TARGET_OS_IOS
        CGContextRef context = UIGraphicsGetCurrentContext();
    #else
        CGContextRef context = [NSGraphicsContext currentContext].CGContext;
    #endif
    CGContextSetStrokeColorWithColor(context, bg10mmColor.CGColor);
    CGContextSetLineWidth(context, bg10mmWidth);
    
    float x = 1.f;
    while (x < self.frame.size.width){
        CGContextMoveToPoint(context, x, 1.f);
        CGContextAddLineToPoint(context, x, self.frame.size.height);
        x += 10 * pixPermm;
    }
    
    float y = 1.f;
    while (y < self.frame.size.height){
        CGContextMoveToPoint(context, 1.f, y);
        CGContextAddLineToPoint(context,self.frame.size.width, y);
        y += 10 * pixPermm;
    }
    CGContextStrokePath(context);
}
    
-(void)initData{
#if TARGET_OS_IOS
    self.layer.backgroundColor = [UIColor clearColor].CGColor;
    background_color = [UIColor whiteColor];
    bg1mmColor = [UIColor colorWithRed:1.f green:0.88f blue:0.88f alpha:1.f];
    bg5mmColor = [UIColor colorWithRed:0.94f green:0.56f blue:0.56f alpha:0.83];
    bg10mmColor = [UIColor colorWithRed:0.94f green:0.56f blue:0.56f alpha:1.f];
#else
    self.wantsLayer = YES;
    self.layer.backgroundColor = [NSColor clearColor].CGColor;
    background_color = [NSColor greenColor];
    background_weak_line_color = [NSColor greenColor];
    background_strong_line_color = [NSColor greenColor];
#endif
    bg5mmWidth = 0.6f;
    bg1mmWidth = 0.6f;
    bg10mmWidth = 0.9f;
    pixPermm = 5.f;
}


@end
